package com.fuhe.chen.vendingmachine.service.impl;

import com.fuhe.chen.vendingmachine.common.MachineCache;
import com.fuhe.chen.vendingmachine.common.redis.RedisUtils;
import com.fuhe.chen.vendingmachine.dao.*;
import com.fuhe.chen.vendingmachine.dto.ItemInMachine;
import com.fuhe.chen.vendingmachine.dto.cond.ItemCond;
import com.fuhe.chen.vendingmachine.pojo.*;
import com.fuhe.chen.vendingmachine.service.ICategoryService;
import com.fuhe.chen.vendingmachine.service.IItemService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

import static com.fuhe.chen.vendingmachine.constant.CacheConstant.ITEMS_ALL_ON_SALE;


@Service
public class ItemServiceImpl implements IItemService {

    @Autowired
    RelationshipItemMachineDao relationshipItemMachineDao;

    @Autowired
    ItemDao itemDao;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    ICategoryService categoryService;

    @Autowired
    ContainerDao containerDao;

    @Override
    public List<ItemInMachine> queryItemGroupByCategoryId(Integer machineID) {
        return null;
    }

    @Override
    public ItemInMachine queryItem(Integer Id) {
        RelationshipItemMachine relationship= relationshipItemMachineDao.findOneById(Id);
        ItemInMachine item = new ItemInMachine(
                relationship.getId(),
                relationship.getItem_id(),
                relationship.getItem().getItem_name(),
                relationship.getItem().getPrice(),
                relationship.getItem().getCategory().getCategoryName(),
                relationship.getItem().getItem_status(),
                relationship.getItem().getPicture_url(),
                relationship.getItem().getSpecifications(),
                relationship.getMachine_id(),
                relationship.getContainer_number());
        return item;
    }

    @Override
    public List<ItemInMachine> queryItem(Integer machineID, Integer itemID) {
        List<RelationshipItemMachine> list= relationshipItemMachineDao.findByCondition(machineID,itemID);
        List<ItemInMachine> items = new ArrayList<>();
        for (RelationshipItemMachine relationship : list) {
            ItemInMachine item = new ItemInMachine(
                    relationship.getId(),
                    relationship.getItem_id(),
                    relationship.getItem().getItem_name(),
                    relationship.getItem().getPrice(),
                    relationship.getItem().getCategory().getCategoryName(),
                    relationship.getItem().getItem_status(),
                    relationship.getItem().getPicture_url(),
                    relationship.getItem().getSpecifications(),
                    relationship.getMachine_id(),
                    relationship.getContainer_number());
            items.add(item);
        }
        return items;
    }

    @Override
    @Transactional
    public String addStock(Integer itemId, Integer machineId, String containerNumber) {

        //商品id检测
        Boolean hasCommodity = false;
        List<Item> items = queryAllOnSale();
        for(Item item : items){
            if(item.getId() == itemId){
                hasCommodity = true;
            }
        }

        if (!hasCommodity){
            return "添加失败,没有找到商品ID";
        }

        //将机器容量减1
        MachineCache.reduceCapacity(machineId);


        RelationshipItemMachine relationship = new RelationshipItemMachine();
        relationship.setItem_id(itemId);
        relationship.setMachine_id(machineId);
        relationship.setContainer_number(containerNumber);
        relationshipItemMachineDao.addRelationship(relationship);

        containerDao.updateStatus(containerNumber,machineId,1);

        return "添加成功";
    }

    @Override
    public List<Item> queryAllOnSale() {
        String key = ITEMS_ALL_ON_SALE;
        if (redisUtils.hasKey(key)){
            return (List<Item>)redisUtils.get(key);
        }else{
            List<Item> items = itemDao.findAllOnSale();
            redisUtils.set(key,items);
            return items;
        }
    }

    @Override
    public void addItem(Item item) {
        itemDao.addItem(item);
        redisUtils.delAll(ITEMS_ALL_ON_SALE);
    }

    @Override
    public void updateItem(Item item) {
        itemDao.updateItem(item);
        redisUtils.delAll(ITEMS_ALL_ON_SALE);
    }

    @Override
    public void deleteItem(Integer item_id) {
        itemDao.deleteItem(item_id);
        redisUtils.delAll(ITEMS_ALL_ON_SALE);
    }

    @Override
    public PageInfo<Item> queryItemByCondition(ItemCond cond, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Item> items = itemDao.findByCondition(cond);
        PageInfo<Item> itemPageInfo=new PageInfo<>(items);
        return itemPageInfo;
    }

    @Override
    public Item queryOne(Integer id) {
        return itemDao.findOne(id);
    }

    @Override
    public List<Category> queryCategories(int machineId) {
        List<RelationshipItemMachine> relationships = relationshipItemMachineDao.findByCondition(machineId, null);
        List<Category> categories = new ArrayList<>();
        for (RelationshipItemMachine relationship : relationships) {
            Category category = relationship.getItem().getCategory();
            if(!categories.contains(category)){
                categories.add(category);
            }
        }
        return categories;
    }

    @Override
    public List<RelationshipItemMachine> queryItemByCategory(int machineId, int categoryId) {
        List<RelationshipItemMachine> relationships = queryItemListByMachineID(machineId);
        List<RelationshipItemMachine> result = new ArrayList<>();
        for (RelationshipItemMachine relationship : relationships) {
            if(relationship.getItem().getItem_category_id()==categoryId){
                result.add(relationship);
            }
        }
        return result;
    }

    @Override
    public List<RelationshipItemMachine> queryItemListByMachineID(int machineId) {
        List<RelationshipItemMachine> relationships = relationshipItemMachineDao.findByCondition(machineId, null);
        return relationships;
    }

}
